package cn.yhjz.platform.system.service.impl;

import cn.yhjz.platform.system.entity.SysUserRole;
import cn.yhjz.platform.system.mapper.SysUserRoleMapper;
import cn.yhjz.platform.system.service.ISysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author maguoping
 * @since 2022-08-11
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    /**
     * 保存一个用户的角色
     *
     * @return
     */
    public int saveUserRole(String userCode, List<String> roleCodeList) {
        int res = 0;
        //首先删除这个用户的所有角色
        int resDelete = this.baseMapper.deleteByUserCode(userCode);
        res += resDelete;
        for (String roleCode : roleCodeList) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserCode(userCode);
            sysUserRole.setRoleCode(roleCode);
            this.baseMapper.insert(sysUserRole);
            res++;
        }
        return res;
    }
}
